package guyubao.com.utils;


import guyubao.com.entity.EmailModel;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Log4j2
@Component
public class SendMailMessageUtils {

    /**
     * 发送者地址
     **/
    @Value("${spring.web.email.sender}")
    private String posterAddress;

    @Resource
    JavaMailSender sender;

    @Resource
    JavaMailSenderImpl javaMailSenderImpl;

    /**
     * 文本发送
     **/
    public void sendEmailAsText(final EmailModel emailModel) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(emailModel.getEmailTheme());
        message.setText(emailModel.getEmailContent());
        message.setFrom(posterAddress);
        message.setTo(emailModel.getReceiverEmailAddress());
        sender.send(message);
    }

    /**
     * html 网页发送
     * 该方法为同步方法
     **/
    public Boolean sendEmailAsSysExceptionHtml(final EmailModel emailModel) {//用final关键字修饰的变量，只能进行一次赋值操作，并且在生存期内不可以改变它的值。
        MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
        MimeMessageHelper mimeMessageHelper;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setSubject(emailModel.getEmailTheme());
            mimeMessageHelper.setFrom(posterAddress);
            mimeMessageHelper.setTo(emailModel.getReceiverEmailAddress());
            mimeMessageHelper.setText(
                    "<!DOCTYPE html>\n" +
                            "<html lang='en'>\n" +
                            "\n" +
                            "<head>\n" +
                            "    <meta charset='UTF-8'>\n" +
                            "    <meta name='viewport' content='width=device-width, initial-scale=1.0'>\n" +
                            "</head>\n" +
                            "\n" +
                            "<body>\n" +
                            "    <div style='background:#f7f7f7;overflow:hidden'>\n" +
                            "        <div style='background:#fff;border:1px solid #ccc;margin:2%;padding:0 30px'>\n" +
                            "            <div style='line-height:40px;height:40px'>&nbsp;</div>\n" +
                            "            <p\n" +
                            "                style='margin:0;padding:0;font-size:14px;line-height:30px;color:#333;font-family:arial,sans-serif;font-weight:bold'>\n" +
                            "                亲爱的用户：</p>\n" +
                            "            <div style='line-height:20px;height:20px'>&nbsp;</div>\n" +
                            "            <p style='margin:0;padding:0;line-height:30px;font-size:14px;color:#333;font-family:' 宋体',arial,sans-serif'>\n" +
                            "                您好！您正在进行邮箱验证，本次请求的验证码为：</p>\n" +
                            "            <p style='margin:0;padding:0;line-height:30px;font-size:14px;color:#333;font-family:' 宋体',arial,sans-serif'>\n" +
                            "                <b style='font-size:18px;color:#f90'>'"+emailModel.getEmailContent()+"'</b><span\n" +
                            "                    style='margin:0;padding:0;margin-left:10px;line-height:30px;font-size:14px;color:#979797;font-family:'\n" +
                            "                    宋体',arial,sans-serif'>(为了保障您帐号的安全性，请在10分钟内完成验证，请勿向他人泄漏验证码信息。)</span>\n" +
                            "            </p>\n" +
                            "            <div style='line-height:80px;height:80px'>&nbsp;</div>\n" +
                            "            <p style='margin:0;padding:0;line-height:30px;font-size:14px;color:#333;font-family:' 宋体',arial,sans-serif'>\n" +"微服务</p>\n" +
                            "        </div>\n" +
                            "    </div>\n" +
                            "<br><br><br>"+
                            "</body>\n" +
                            "\n" +
                            "</html>"
                    , true);

            sender.send(mimeMessage);
            log.info("邮件已发送至[{}]邮箱！", emailModel.getReceiverEmailAddress());
            return true;
        } catch (MessagingException e) {
            log.error("邮件发送失败：{}", e.getMessage());
            return false;
        } catch (MailException e) {
            log.error("邮箱异常：{}", e.getMessage());
            return false;
        }
    }
}